/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ca.usask.cs.decisiontree;

import javax.swing.JOptionPane;

import ca.usask.cs.decisiontree.api.Edge;
import ca.usask.cs.decisiontree.api.TerminalNodeInterface;

/**
 *@editer WenyiAn
 * @author Travis
 * @editor Wenyi An
 */
public class TerminalNode implements TerminalNodeInterface {

    private Edge sourceEdge;
    private double rollBackValue;
    private String variableName;
    private double time;
    private boolean selected;
    private int treeNodeID;
    private String NodeType = "TerminalNode";
    /**
     * Constructor that allows the user to not specify a source edge on creation
     */
    public TerminalNode(int theTreeNodeID)
    {
        this.sourceEdge = null;
        this.rollBackValue = Double.NaN;
        this.time = Double.NaN;
        this.selected = false;
        this.treeNodeID = theTreeNodeID;
    }
    /**
     * return the type of Node
     * @return NodeType
     */
    public String getNodeType(){
    	return this.NodeType;
    }

    /**
     * retrieves the treeNode ID
     * @return treeNodeID in int
     */
    public int getTreeNodeID()
    {
        return this.treeNodeID;
    }

    /**
     * Retrieves the variable name of the chance node
     * @return the variable name in string format
     */
    public String getVariableName()
    {
        return this.variableName;
    }

    /**
     * Assigns the Variable name of the intermediate tree node
     */
    public void setVariableName(String newVariableName)
    {
        this.variableName = newVariableName;
    }

    /**
     * Indicate if the edge is selected
     * 
     * @return  true if selected
     *          false if unselected
     */
    public boolean isSelected()
    {
        return this.selected;
    }

        /**
     * Assigns he edge as selected or not selected
     */
    public void setSelected(boolean newSelected)
    {
        this.selected = newSelected;
    }

    /**
     * Retrieves the time of the Edge
     * @return  time in the form of a double
     */
    public double getTime()
    {
        return this.time;
    }

    /**
     * Retrieve the Edge which connects this TreeNode to its parent TreeNode.
     * 
     * @return An Edge with this TreeNode as its destination. Will return NULL if no source Edge exists.
     * @see Edge
     */
    public Edge getSourceEdge()
    {
        return sourceEdge;
    }

    /**
     * Assigns the time of the Edge as the newTime
     *
     * @precondition newTime must be > = 0
     */
    public void setTime(double newTime) throws IllegalArgumentException
    {
        if(newTime < 0)
        {
        	JOptionPane.showMessageDialog(null,"The new time is not valid (less than 0)");
            throw new IllegalArgumentException("The new time is not valid (less than 0)");
        }
        this.time = newTime;
    }

    /**
     * Set a new source Edge for this TreeNode. The Edge must have this TreeNode as its destination.
     * 
     * @precondition getSourceEdge is not null
     * @precondition getSourceEdge has this node as destination
     * @precondition getSourceEdge does not haev destination and source as same
     * @postcondition getSourceEdge returns the value provided as an argument
     * 
     * @param sourceEdge An Edge with this node as the destination.
     * @see Edge
     */
    public void setSourceEdge(Edge newSourceEdge) throws IllegalArgumentException
    {
        if(newSourceEdge == null
                || newSourceEdge.getDestinationNode() != this
                || newSourceEdge.getSourceNode() == newSourceEdge.getDestinationNode())
        {
            throw new IllegalArgumentException("The source edge \"" + newSourceEdge +  "\" provided is not valid.");

        }
        
        this.sourceEdge = newSourceEdge;
    }

    /**
     * method to return terminal value
     * @return terminal value in double
     */
    public double getRollBackValue()
    {
        return this.rollBackValue;
    }

    /**
     * method to set terminal value
     * @param double that represents terminal value
     */
    public void setRollBackValue(double newValue)
    {
        this.rollBackValue = newValue;
    }
	@Override
	public void DecisionNodeCheck() {
		// TODO Auto-generated method stub
		 throw new IllegalArgumentException("Source Node of a basicaEdge cannot be a Terminal Node");
	}
	@Override
	public void notTerminalNodeCheck(String msg) {
		// TODO Auto-generated method stub
		//  JOptionPane.showMessageDialog(null,"The Root Node cannot be a terminal node");
          throw new IllegalArgumentException(msg);
	}
	@Override
	public void ChanceNodeCheck(String msg) {
		// TODO Auto-generated method stub
		throw new IllegalArgumentException(msg);
	}

}
